package com.bbcare.exportExcel.plat.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bbcare.comm.AppException;
import com.bbcare.comm.Constants;
import com.bbcare.comm.DateUtil;
import com.bbcare.comm.ErrorCode;
import com.bbcare.comm.ResultModel;
import com.bbcare.comm.ToolUtils;
import com.bbcare.department.plat.controller.DepartmentBasicController;
import com.bbcare.exportExcel.plat.service.IUploadExcelService;

/**
 * @name: 数据导出
 * @author: wuyz
 * @createTime: 2018.1.12
 * @description:
 * @modify:
 *
 */
@Controller
@RequestMapping("/service/exportExcel")
public class UploadExcel {
	Logger logger = Logger.getLogger(DepartmentBasicController.class);
    @Autowired
    private IUploadExcelService iUploadExcelService;

    @ResponseBody
    @RequestMapping("/updaload")
    public ResultModel<Map<String,Object>> updaload(HttpServletRequest request, @RequestBody String jsonStr)
            throws Exception {
        logger.info("in updaload----arg==" + jsonStr);
        String  currTime = DateUtil.getTimeString(DateUtil.getSysDateA());// 创建时间
        JSONObject jsonEsb = JSON.parseObject(jsonStr);
        JSONObject requestParam = jsonEsb.getJSONObject(Constants.INF_INPUTPARAM_COMMONFIELD); // 统一节点处理
        if (null == requestParam) {
            throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参节点错误，无data节点！");
        }

        String projectName = requestParam.getString("projectName");
        if(StringUtils.isEmpty(projectName)){
        	throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR,"入参校验失败，项目名称为空");
        }

        String projectId = requestParam.getString("projectId");
        if(StringUtils.isEmpty(projectId)){
        	throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR,"入参校验失败，项目id为空");
        }

        Map<String, Object> MapOut = new HashMap<String,Object>();
        Map<String, Object> MapIn = new HashMap<String,Object>(requestParam);
        // 创建人，从session中获取
        Map<String, Object> sessionMap = ToolUtils.getSessionInfo(request);
        String tenantId = (String) sessionMap.get("tenantId");// 租户id
        String deptId = (String) sessionMap.get("deptId");// 科室id
        String authorName = (String) sessionMap.get("authorName");// 医生姓名
        String authorId = (String) sessionMap.get("authorId");// 医生id
        MapIn.put("talentId", tenantId);
        MapIn.put("deptId", deptId);
        MapIn.put("authorName",authorName);
        MapIn.put("authorId", authorId);
        MapIn.put("createTime", currTime);
        MapIn.put("updateTime", currTime);

        if(Constants.PATIENT_DATA_TYPE_LEHUI.equals(projectId)) {
        	 iUploadExcelService.queryLehuiAll(MapIn);
        }else if(Constants.PATIENT_DATA_TYPE_BEILI.equals(projectId)) {
        	iUploadExcelService.queryBreastBeili(MapIn);
        }else if(Constants.PATIENT_DATA_TYPE_LEHUIJL.equals(projectId)) {
        	iUploadExcelService.queryLehui(MapIn);
        }else if(Constants.PATIENT_DATA_TYPE_MRWY.equals(projectId)) {
        	iUploadExcelService.queryBreastFeed(MapIn);
        }else if(Constants.PATIENT_DATA_TYPE_YYJL.equals(projectId)) {
        	iUploadExcelService.querAnxious(MapIn);
        }else if(Constants.PATIENT_DATA_TYPE_EBZH.equals(projectId)) {
        	iUploadExcelService.queryCareBaby(MapIn);
        }else if(Constants.PATIENT_DATA_TYPE_LCZS.equals(projectId)){
        	iUploadExcelService.queryLCZS(MapIn);
        }
        return new ResultModel<Map<String,Object>>(MapOut);
    }

    //获取下载的文件
    @ResponseBody
    @RequestMapping("/getURLToUpload")
    public ResultModel<List<Map<String, Object>>> getURLToUpload(HttpServletRequest request, @RequestBody String jsonStr)
            throws Exception {
        logger.info("in getURLToUpload----arg==" + jsonStr);

        JSONObject jsonEsb = JSON.parseObject(jsonStr);
        JSONObject requestParam = jsonEsb.getJSONObject(Constants.INF_INPUTPARAM_COMMONFIELD); // 统一节点处理
        if (null == requestParam) {
            throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参节点错误，无data节点！");
        }

        String projectName = requestParam.getString("projectName");
        if(StringUtils.isEmpty(projectName)){
        	throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR,"入参校验失败，项目名称为空");
        }

        String projectId = requestParam.getString("projectId");
        if(StringUtils.isEmpty(projectId)){
        	throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR,"入参校验失败，项目id为空");
        }

        String beginTime = requestParam.getString("beginTime");
        if(StringUtils.isEmpty(beginTime)){
        	throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR,"入参校验失败，开始时间为空");
        }

        String endTime = requestParam.getString("endTime");
        if(StringUtils.isEmpty(endTime)){
        	throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR,"入参校验失败，结束时间为空");
        }

        Map<String, Object> MapIn = new HashMap<String,Object>(requestParam);
        // 创建人，从session中获取
        Map<String, Object> sessionMap = ToolUtils.getSessionInfo(request);
        String tenantId = (String) sessionMap.get("tenantId");// 租户id
        String deptId = (String) sessionMap.get("deptId");// 科室id
      //  String authorName = (String) sessionMap.get("authorName");// 医生姓名
       // String authorId = (String) sessionMap.get("authorId");// 医生id
        MapIn.put("talentId", tenantId);
        MapIn.put("deptId", deptId);
       // MapIn.put("authorName",authorName);
       // MapIn.put("authorId", authorId);
        List<Map<String, Object>> listOut = iUploadExcelService.getURLToUpload(MapIn);
        return new ResultModel<List<Map<String, Object>>>(listOut);
    }

    //获取下载的文件
    @ResponseBody
    @RequestMapping("/getURLToUploadDate")
    public Map<String, Object> getURLToUploadDate(HttpServletRequest request, @RequestBody String jsonStr)
            throws Exception {
        logger.info("in getURLToUploadDate----arg==" + jsonStr);

        JSONObject jsonEsb = JSON.parseObject(jsonStr);
        JSONObject requestParam = jsonEsb.getJSONObject(Constants.INF_INPUTPARAM_COMMONFIELD); // 统一节点处理
        if (null == requestParam) {
            throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参节点错误，无data节点！");
        }

        Map<String, Object> MapIn = new HashMap<String,Object>(requestParam);
        // 创建人，从session中获取
        Map<String, Object> sessionMap = ToolUtils.getSessionInfo(request);
        String tenantId = (String) sessionMap.get("tenantId");// 租户id
        String deptId = (String) sessionMap.get("deptId");// 科室id
      //  String authorName = (String) sessionMap.get("authorName");// 医生姓名
       // String authorId = (String) sessionMap.get("authorId");// 医生id
        MapIn.put("talentId", tenantId);
        MapIn.put("deptId", deptId);
        // 针对Datatable特殊处理
        String jsonStr1 = requestParam.getString(Constants.INF_INPUTPARAM_DATATABLE_FIELD);
        int draw = 0;
        if (!StringUtils.isEmpty(jsonStr1)) {
            JSONObject jsonTable = (JSONObject) JSONObject.parse(jsonStr1);
            draw = jsonTable.getIntValue("draw");
            MapIn.put("start", jsonTable.getIntValue("start"));
            MapIn.put("end", jsonTable.getIntValue("length"));
        }
       // MapIn.put("authorName",authorName);
       // MapIn.put("authorId", authorId);
        Map<String, Object> retMap = iUploadExcelService.getURLToUploadDate(MapIn);
        // 针对Datatable特殊处理
        if (!StringUtils.isEmpty(jsonStr)) {
        	retMap.put("draw", draw);
        	retMap.put("recordsTotal", retMap.get("total"));
        	retMap.put("recordsFiltered", retMap.get("total"));
        }
        retMap.put("success", true);
        return retMap;
    }
    //获取当前租户下配置那些导出项目
    @ResponseBody
    @RequestMapping("/gettenantIdforProject")
    public ResultModel<List<Map<String, Object>>> gettenantIdforProject(HttpServletRequest request, @RequestBody String jsonStr)
            throws Exception {
        logger.info("in getURLToUpload----arg==" + jsonStr);

        JSONObject jsonEsb = JSON.parseObject(jsonStr);
        JSONObject requestParam = jsonEsb.getJSONObject(Constants.INF_INPUTPARAM_COMMONFIELD); // 统一节点处理
        if (null == requestParam) {
            throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参节点错误，无data节点！");
        }

        Map<String, Object> MapIn = new HashMap<String,Object>(requestParam);
        // 创建人，从session中获取
        Map<String, Object> sessionMap = ToolUtils.getSessionInfo(request);
        String tenantId = (String) sessionMap.get("tenantId");// 租户id
        String deptId = (String) sessionMap.get("deptId");// 科室id
      //  String authorName = (String) sessionMap.get("authorName");// 医生姓名
       // String authorId = (String) sessionMap.get("authorId");// 医生id
        MapIn.put("tenantId", tenantId);
        MapIn.put("deptId", deptId);
        MapIn.put("type", Constants.ASSESSMENT_TENANTID_TYPE);
       // MapIn.put("authorName",authorName);
       // MapIn.put("authorId", authorId);
        List<Map<String, Object>> listOut = iUploadExcelService.gettenantIdforProject(MapIn);
        return new ResultModel<List<Map<String, Object>>>(listOut);
    }

}
